package com.example.myapplication.model;

import android.util.Log;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MedicalRecordFetcher {

    public static List<List<Object>> getMedicalRecords(String phoneNumber) {
        List<List<Object>> medicalRecords = new ArrayList<>();

        OkHttpClient client = new OkHttpClient();
        String url = "http://39.107.92.86:5000/querymedicalrecord?phone_number=" + phoneNumber;

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String jsonData = response.body().string();
                JSONArray jsonArray = new JSONArray(jsonData);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray recordArray = jsonArray.getJSONArray(i);
                    List<Object> record = new ArrayList<>();
                    for (int j = 0; j < recordArray.length(); j++) {
                        record.add(recordArray.get(j));
                    }
                    medicalRecords.add(record);
                }
            } else {
                // 处理请求失败情况
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

        return medicalRecords;
    }
}
